unit ToolOptionUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OptionBaseUnit, StdCtrls, ComCtrls, Menus, ImgList, ADODB, DB,
  Buttons;

type
  TfrmToolOption = class(TfrmOptionBase)
    lvToolOption: TListView;
    PopupMenu1: TPopupMenu;
    mniAdd: TMenuItem;
    mniEdit: TMenuItem;
    mniDel: TMenuItem;
    ilToolOption: TImageList;
    procedure mniAddClick(Sender: TObject);
    procedure mniEditClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    procedure ClearTool;
  public
    { Public declarations }
    procedure SaveOption; override;
    procedure LoadOption; override;
  end;

var
  frmToolOption: TfrmToolOption;

implementation

uses ToolOptionEditUnit, MainUnit, FunctionUnit, ConstUnit;

{$R *.dfm}

{ TfrmToolOption }

procedure TfrmToolOption.LoadOption;
var
  Sql, IconFile: string;
  Query: TADOQuery;
  li: TListItem;
  bmp, bmp1: TBitmap;
begin
  inherited;
  ClearTool;
  Sql := 'SELECT * FROM Tools';
  Query := TADOQuery.Create(Self);
  try
    OpenDataSet(Sql, Query, Database);
    while not Query.Eof do
    begin
      li := lvToolOption.Items.Add;
      li.Caption := Query.FieldByName('ToolName').AsString;
      li.SubItems.Add( Query.FieldbyName('ToolServerPath').AsString );
      li.SubItems.Add( Query.FieldByName('ToolMainExe').AsString );
      li.SubItems.Add( Query.FieldByName('ToolClientPath').AsString );
      li.SubItems.Add( Query.FieldByName('ToolVersion').AsString );
      IconFile := ExtractFilePath(ParamStr(0)) + Query.FieldByName('ToolIcon').AsString;
      if not FileExists( IconFile ) then
        IconFile := ExtractFilePath(ParamStr(0)) + UnknownIcon;
      bmp := TBitmap.Create;

      bmp.LoadFromFile(IconFile);
      li.Data := Pointer( bmp );
      bmp1 := TBitmap.Create;
      try
        bmp1.Width := ilToolOption.Width;
        bmp1.Height := ilToolOption.Height;
        bmp1.Canvas.StretchDraw(Rect(0, 0, bmp1.Width, bmp1.Height), bmp);
        li.ImageIndex := ilToolOption.Add(bmp1, nil);
      finally
        bmp1.Free;
      end;

      Query.Next;
    end;
  finally
    Query.Free;
    Database.Close;
  end;

end;

procedure TfrmToolOption.SaveOption;
var
  Sql: string;
  Query: TADOQuery;
  i, ID: Integer;
  li: TListItem;
begin
  inherited;
  Sql := 'SELECT MAX(ToolId) From Tools';
  Query := TADOQuery.Create(Self);
  try
    OpenDataSet(Sql, Query, Database);
    if Query.Fields[0].IsNull then
      ID := 1
    else
      ID := Query.Fields[0].AsInteger + 1;

    Sql := 'DELETE FROM Tools';
    ExecuteSql( Sql, Database );
    Sql := 'SELECT * FROM Tools WHERE 1=2';
    OpenDataSet(Sql, Query, Database);
    for i := 0 to lvToolOption.Items.Count - 1 do
    begin
      li := lvToolOption.Items[i];
      Query.Append;
      Query.FieldByName('ToolId').Value := Id;
      Query.FieldByName('ToolName').Value := li.Caption;
      Query.FieldByName('ToolServerPath').Value := li.SubItems[0];
      Query.FieldByName('ToolMainExe').Value := li.SubItems[1];
      Query.FieldByName('ToolClientPath').Value := li.SubItems[2];
      Query.FieldByName('ToolVersion').Value := li.SubItems[3];
      Query.FieldByName('ToolIcon').Value := SoftwareIconFolder + Format('tl%d.bmp', [ID]);
      TBitmap(li.Data).SaveToFile(ExtractFilePath(ParamStr(0)) + SoftwareIconFolder + Format('tl%d.bmp', [ID]));
      Query.CheckBrowseMode;
      Inc( ID );
    end;
  finally
    Query.Free;
    Database.Close;
  end;
end;

procedure TfrmToolOption.mniAddClick(Sender: TObject);
var
  li: TListItem;
  bmp, bmp1: TBitmap;
begin
  inherited;
  with TfrmToolOptionEdit.Create(Self) do
  try
    if ShowModal = IdOK then
    begin
      li := lvToolOption.Items.Add;
      li.Caption := edtName.Text;
      li.SubItems.Add(edtServerPath.Text);
      li.SubItems.Add(edtMainExe.Text);
      li.SubItems.Add(edtClientPath.Text);
      li.SubItems.Add(edtVersion.Text);
      bmp := TBitmap.Create;
      bmp.Assign( imgIcon.Picture.Bitmap );
      bmp1 := TBitmap.Create;
      try
        bmp1.Width := ilToolOption.Width;
        bmp1.Height := ilToolOption.Height;
        bmp1.Canvas.StretchDraw(Rect(0, 0, bmp1.Width, bmp1.Height), bmp);
        li.ImageIndex := ilToolOption.Add(bmp1, nil);
      finally
        bmp1.Free;
      end;   
      li.Data := Pointer( bmp );
      li.SubItems.Add('');
    end;
  finally
    Free;
  end;   
end;

procedure TfrmToolOption.ClearTool;
var
  i: integer;
begin
  for i := 0 to lvToolOption.Items.Count - 1 do
    TObject(lvToolOption.Items[i].Data).Free;
  lvToolOption.Clear;
end;

procedure TfrmToolOption.mniEditClick(Sender: TObject);
var
  li: TListItem;
  bmp: TBitmap;
begin
  inherited;
  if lvToolOption.Selected = nil then
    Exit;
  li := lvToolOption.Selected;  
  with TfrmToolOptionEdit.Create(Self) do
  try  
    edtName.Text := li.Caption;
    edtServerPath.Text := li.SubItems[0];
    edtMainExe.Text := li.SubItems[1];
    edtClientPath.Text := li.SubItems[2];
    edtVersion.Text := li.SubItems[3];
    imgIcon.Picture.Bitmap.Assign(TBitmap( li.Data ));
    if ShowModal = IdOk then
    begin
      li.Caption := edtName.Text;
      li.SubItems[0] := edtServerPath.Text;
      li.SubItems[1] := edtMainExe.Text;
      li.SubItems[2] := edtClientPath.Text;
      li.SubItems[3] := edtVersion.Text;
      TBitmap(li.Data).Assign(imgIcon.Picture.Bitmap);
      bmp := TBitmap.Create;
      try
        bmp.Width := ilToolOption.Width;
        bmp.Height := ilToolOption.Height;
        bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), TBitmap(li.Data));
//        ilToolOption.Delete(li.ImageIndex);
        li.ImageIndex := ilToolOption.Add(bmp, nil);
      finally
        bmp.Free;
      end;  
    end;
  finally
    Free;
  end;

end;

procedure TfrmToolOption.FormCreate(Sender: TObject);
begin
  inherited;
  LoadOption;
end;

procedure TfrmToolOption.FormDestroy(Sender: TObject);
begin
  ClearTool;
  inherited;
end;

end.
